/**
 * @version      BOSSCMS-Free
 * @trademark    BOSSCMS
 * @copyright    Copyright © by BOSSCMS-Free (https://www.bosscms.net/) 保留所有权利
 * @license      https://gitee.com/Greenpeas/BOSSCMS/license/ 和 https://www.bosscms.net/freelicense/
 * @link         https://www.bosscms.net/
 * @author       温州软界网络技术有限公司
**/
$(document).ready(function(e){		
	if(hs = window.parent.location.hash.match(/#mpf=\w+(\/(?:\w|%2C)+(,[^#]*){0,1})*/)){
		hs = hs[0].replace('#mpf=','');
		var pm = hs.split('/');
		pm0 = pm[0]&&pm[0].indexOf(',')<0?pm[0]:null;
		pm1 = pm[1]&&pm[1].indexOf(',')<0?pm[1]:null;
		pm2 = pm[2]&&pm[2].indexOf(',')<0?pm[2]:null;
		para = {};
		for(i=1;i<pm.length;i++){
			if(pm[i].indexOf(',')>0){
				p = pm[i].split(',');
				para[p[0].replace(/%2C/g,',')] = p[1];
			}
		}
		$('section.content iframe').attr('src',	$.mpf(mold=pm0, part=(pm1?pm1:pm0), func=(pm2?pm2:'init'), para));
	}else{
		$('section.content iframe').attr('src',	$.mpf('home', 'home', 'init'));
	}


	$(document).on('click','.snav .fa-refresh',function(){
		$('iframe[name=iframe]').contents()[0].location.reload();
	});
	
	$('header.topnav>.column>ul>li').click(function(){
		$('header.topnav>.column>ul>li').removeClass('on');
		$(this).addClass('on');
		$('section.category>.nav>ul').removeClass('on');
		nav = $(this).attr('class').match(/nav(\d+)/)[1];
		$('section.category>.nav>ul[nav='+nav+']').addClass('on');
		$('section.content iframe').attr('src',	$('section.category>.nav>ul[nav='+nav+']>li:eq(0)>ul>li:eq(0)>a').attr('href'));
	});

	$('header.topnav>.menu').click(function(){
		if($(this).hasClass('on')){
			$(this).removeClass('on');
			$('section.category>.nav').removeClass('active');
		}else{
			$(this).addClass('on');
			$('section.category>.nav').addClass('active');
		}
	});

	$('section.category>.nav>ul>li>ul>li>a[target="iframe"]').click(function(){
		$('header.topnav>.menu').click();
	});
	
	$('header.topnav>.wrap>ul>li.cache a').click(function(){
		$.get($(this).attr('url'),function(data){
			if(data.state=='success'){
				_alert(data.msg,'green');
			}else{
				_alert('操作失败','red');
			}
		},'json');
	});
	
	$('section.category div.nav li').mouseover(function(){
		if($('section.category.bosscms, header.topnav, section.content').hasClass('icon')){
			$(this).find('ul').css('top', $(this).offset().top+5);
		}
	});
	
	$('section.category>.nav>ul>li>a').click(function(){
		li = $(this).parent('li');
		if(li.hasClass('on')){
			li.removeClass('on');
		}else{
			$('section.category>.nav>ul>li').removeClass('on');
			li.addClass('on');
		}
	});

	$('section.category>.nav a.shrink').click(function(){
		if($('section.category.bosscms, header.topnav, section.content').hasClass('icon')){
			$('section.category.bosscms, header.topnav, section.content').removeClass('icon');
			$.setCookie('IframeIconOpen',0);
		}else{
			$('section.category.bosscms, header.topnav, section.content').addClass('icon');
			$.setCookie('IframeIconOpen',1);
		}
	});
	prompt = $('header.topnav>.wrap .buy .update>u');
	upnow = true;
	$('header.topnav>.wrap .buy:not(.on) .update>a.green[href]').click(function(){
		$.post('https://api.bosscms.net/rest/user/identity.php?auth=free',{'user_sequence':$('section.content iframe')[0].contentWindow.$G['user']},function(data){
			if(data.length!=0){
				if(upnow){
					upnow = false;
					$('header.topnav>.wrap .buy').addClass('on');
					prompt.html('<i class="fa fa-spinner"></i>正在下载并解压文件...');
					$.get($.mpf('update','update','download',{'type':'plus','name':'free'}),function(res){
						if(res=='ok'){
							prompt.html('<i class="fa fa-spinner"></i>正在执行安装文件...');
							$.get($.mpf('update','update','install'),function(ins){
								if(ins=='ok'){
									_alert('安装完成','green');
									re('auto');
								}else{
									_alert(ins?ins:'安装失败');
									re('');
								}
							});
						}else{
							_alert(res);
							re('');
						}
					});
				}
			}else{
				if($('section.content iframe').attr('src')!=$.mpf('template', 'market', 'init')){
					$('section.content iframe').attr('src',	$.mpf('template', 'market', 'init'));
				}
				_alert('请登录官方账号');
			}
		},'json');
	});
});
function re(has){
	prompt.html('<i class="fa fa-spinner"></i>三秒后刷新当前窗口...');
	window.location.hash = has;
	window.setTimeout(function(){
		window.location.reload();
	},3000);
}